/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.model;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

/**
 * This class represents a flight company and its full <b>state</b>. 
 * <p>
 * 
 * Basically this class represents the state of the application
 * which is basically a collection of flights indexed by their
 * id and each flight has information about their reservations.
 * <p>
 * 
 * Warning: this class is not thread safe, but if you use it from 
 * one single server instance this is not a problem.
 *  
 * @author drodenas
 */
public final class Company implements Serializable {
	private static final long serialVersionUID = -9028448843439946692L;
	protected Company() { serverId = null; }

	private final Map<FlightId,Flight> flights = new TreeMap<>();
    private final ServerId serverId; // for logging
    
    Company(ServerId server) {
        this.serverId = server;
    }
    
    /**
     * This is the operation that you should use to get a flight.
     * <p>
     * 
     * In this operation you provide a flight id and it will recover
     * the exact flight object that you have requested.
     * If the flight requested does not exists the flight is created
     * automatically. 
     * <p>
     * 
     * If you modify a flight, next time that you request the same
     * flight you will obtain the same flight with the same modifications.
     * 
     * @param flightId the flight id of the flight that you want to recover that belongs to this company
     * @return the corresponding flight, never null
     */
    public Flight getFlight(FlightId flightId) {
        if (flightId == null) throw new NullPointerException("Illegal argument: flightId cannot be null");
        
        Flight flight = flights.get(flightId);
        if (flight == null) {
            flight = new Flight(serverId, flightId);
            flights.put(flightId, flight);
        }
        return flight;
    }

    @Override
    public String toString() {
        return "Company="+flights.values();
    }
    
}
